<?php
include 'requireLogin.php';
include 'connectdb.php';
error_reporting(E_ALL);


/** PHPExcel */
require_once 'Classes/PHPExcel.php';

$getExcel = "SELECT * FROM bill";
$res = mysql_query($getExcel);
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

if(!$res){
    die("Error");
}

$array = array("Mã", "Mã sản phẩm", "Giá tiền", "Số lượng", "Mã khách hàng", "Mã Nhân viên", "Khuyến mại", "Ngày giao dịch","Tổng");
for ($col = 0; $col < 10; $col++)
	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $array[$col]);

$row=2;
while($mrow = mysql_fetch_assoc($res)) { 
    $col = 0; $index = 0;
    foreach($mrow as $key=>$value) { 
    	if (!($col == 1 || $col == 8 || $col == 11))
    	{
        	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($index, $row, $value); 
        	$index++;
        }
        $col++; 
    } 
    $row++; 
} 

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Save Excel 2003 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('tong_hoa_don.xls');
header("Location:tong_hoa_don.xls");
?>